<template>
  <el-dialog
    title="角色分配"
    :visible.sync="dialogShow"
  >
    <el-checkbox-group v-model="checkedList">
      <el-checkbox v-for="item in allRoleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <span slot="footer">
      <el-button @click="dialogShow=false">取消</el-button>
      <el-button type="primary" @click="submitFn">提交</el-button>
    </span>
  </el-dialog>

</template>

<script>
import { getRoleList } from '@/api/setting.js'
import { getUserDetail } from '@/api/user.js'
import { assignRoles } from '@/api/employees.js'
export default {
  name: 'SetRole',
  data() {
    return {
      dialogShow: false,
      checkedList: [], // 回显已选中的角色
      allRoleList: [], // 所有的角色权限
      userId: ''
    }
  },
  methods: {
    async getRoleList() { // 获取所有角色列表
      const res = await getRoleList({
        page: 1,
        pageSize: 100
      })
      console.log(res, '全部=====')
      this.allRoleList = res.rows// 赋值所有的角色
    },
    // 获取员工个人角色列表
    async getuserDetail(id) {
      this.userId = id
      const res = await getUserDetail(id)
      console.log(res, '个人====')
      this.checkedList = res.roleIds// 将当前用户的角色赋值给checkedList,用来回显
      // console.log(this.checkedList)
    },
    // 提交角色修改
    async submitFn() { // 点击提交按钮更新修改角色权限
      const res = await assignRoles({
        id: this.userId,
        roleIds: this.checkedList || []
      })
      console.log(res)
      this.$message.success('操作成功')
      this.dialogShow = false
    }
  }

}
</script>

<style>

</style>
